<?php
//рус
include_once(_APP_PATH. 'modules/admin/controllers/media/media_admin_http.php');
class media_admin_http_web extends media_admin_http
{   
    protected function ControllerInit ()
    {
		$this->InitExtMode();
    }    
    
    protected function PrimaryActionInit ($basicActionName)
    {
		$this->_module->GetDecorator()->SetMenuActiveElement('media/list');	   		
    }    
    
    /**
		экшен по-умолчанию
	*/
    public function PrAction ()
    {
		$this->out_mediaPr();        
    }  

    public function out_mediaPrAction()
    {
    	$this->GetQueryPage();

        $this->actionTitle = 'Медиа файлы';

        $filter = $this->GetFilter();       

        $onPage = MozgCfg::GetProp('admin/media//perPage');
               
        $paginator = SysLoader::GetModule('controlls', 'http', '_web')->GetCtrl('paginator');
        $pattern = MozgRoute::Get('http')->Create(array('module' => 'admin', 'ctrl' => $this->GetSelfName(), 'out_media', 'page' => '__page__'));
        $paginator->SetUrlPattern($pattern);         
        $pagMapper = $paginator->GetMapper();
        $pagMapper->Set(0, $this->_page, $onPage);
        
        $files = $this->GetMediaMapper()->GetMedia($pagMapper, $filter);
        $pagMapper->SetTotalCount($this->GetMediaMapper()->GetTotal());               
        
        $t = $this->GetTemplator();
        $t->ctrlName = $this->GetSelfName();
        $t->files = $files;
        $t->extId = $this->GetExtId();
        $t->extMod = $this->GetExtMod();
        $t->thumbFolder = $this->GetPhotoThumbFolder('mediathumbnail');
        $t->page = $this->_page;
        $t->pages = $paginator->Generate();
        $this->LoadTemplate($t->render('media/_media_files.phtml'));
        
        $this->GetModule()->GetDecorator()->SetSecondAction('Left Sidebar', 'OutFilterInBlock', $this->GetSelfName());		
    	$this->GetModule()->GetDecorator()->SetSecondAction('Right Sidebar', 'OutExtModInBlock', $this->GetSelfName());
    } 

    public function add_mediaPrAction($file = null)
    {
        $this->actionTitle = 'Медиа файлы';

        if (!$file)
        {
        	$file = $this->GetMediaFromQueryId();
	   		if (!$file) $file = $this->GetMediaMapper()->GetNew();
        }        

        $mediaHTML = '';
        if ($file->GetId())
        {
        	$media = SysLoader::GetModule('controlls', 'http', '_web')->GetCtrl('media');
        	$mediaHTML = $media->Get($file);
        }
        
        $t = $this->GetTemplator();
        $t->ctrlName = $this->GetSelfName();
        $t->mediaHTML = $mediaHTML;
        $t->file = $file;
        $t->thumbFolder = $this->GetPhotoThumbFolder('mediathumbnail');
        $this->LoadTemplate ($t->render('media/_addmedia.phtml'), '99%');  

        $this->_module->GetDecorator()->SetMenuActiveElement('media/add');
    }
    
    public function save_mediaPrAction()
    {
       	$file = $this->GetMediaFromQueryId();
   		if (!$file) $file = $this->GetMediaMapper()->GetNew();	

   		$file->SetPublishDate(trim($this->Request()->getPost('publish_date')));
        $file->SetTitle(trim($this->Request()->getPost('title')));
        $file->SetDescription(trim($this->Request()->getPost('description')));
        $file->SetTags(trim($this->Request()->getPost('tags')));
        
        if (!$file->GetId())
        {
        	$fileUploadType = $this->Request()->getPost('select_file_input_type');
	        if ($fileUploadType == 'd')
	        {
	        	$uploadedFile = $this->Request()->getFile('media_file');                   
				$file->SetTempFile($uploadedFile);
	        }
	        else
	        {
	        	$uploadedFile = $this->Request()->getPost('media_file_server');  
				$file->SetLocalUrlFile($uploadedFile);	        	
	        }	         
        }
        
		if ($this->GetMediaMapper()->Save($file)) 
        {	  
            $this->Libs()->messanger->createMessage('Данные сохранены');
	       	MozgRoute::Get('http')->Redirect(array('module' => 'admin', 'ctrl' => 'media'));  
        } 
        else
        {
        	if ($this->GetMediaMapper()->IsError())
        	{
        		$this->Libs()->messanger->createMessage($this->GetMediaMapper()->GetErrorMessage(), MESSAGE_ERROR);
        	}
        	else
        	{
	            $this->Libs()->messanger->createMessage('Данные не были сохранены', MESSAGE_ERROR);     		
        	}
        	
        	$this->add_mediaPr($file);
            return;
        }    	
    }
    
    public function del_mediaPrAction()
    {
       	$file = $this->GetMediaFromQueryId();
   		if (!$file)
   		{
   			$this->Libs()->messanger->createMessage('Файл не найден', MESSAGE_ERROR);
			MozgRoute::Get('http')->Redirect(array('module' => 'admin', 'ctrl' => 'media'));      				   		
   		}   

   		if ($this->GetMediaMapper()->Delete($file->GetId()))
   		{
			$this->Libs()->messanger->createMessage('Файл удалён');   			
   		}
   		else
   		{
			$this->Libs()->messanger->createMessage('Не удалось удалить файл', MESSAGE_ERROR);   			
   		}
   		
		MozgRoute::Get('http')->Redirect(array('module' => 'admin', 'ctrl' => 'media'));    	
    }    
    
    public function save_ext_mediaPrAction()
    {
        $extId = $this->GetExtId();
        $extMod = $this->GetExtMod();    	
    	
        if (!$extId)
        {
   			$this->Libs()->messanger->createMessage('Не найден элемент', MESSAGE_ERROR);
			MozgRoute::Get('http')->Redirect(array('module' => 'admin', 'ctrl' => 'media'));         	
        }
        
        $mediaFileIds = (array)$this->Request()->getPost('media_id');
        $this->GetMediaMapper()->AddExtMedia($extId, $extMod, $mediaFileIds);
        
        MozgRoute::Get('http')->Redirect(array('module' => 'admin', 'ctrl' => $this->GetSelfName(), 'out_media', 'ext_id' => $extId, 'ext_mod' => $extMod));
    }
    
    public function del_ext_mediaPrAction()
    {
        $extId = $this->GetExtId();
        $extMod = $this->GetExtMod();    	
    	
        if (!$extId)
        {
   			$this->Libs()->messanger->createMessage('Не найден элемент', MESSAGE_ERROR);
			MozgRoute::Get('http')->Redirect(array('module' => 'admin', 'ctrl' => 'media'));          	
        }
        
		$mediaId = (int)$this->Request()->getQuery('media_id');
		$this->GetMediaMapper()->DelExtMedia($extId, $extMod, $mediaId);
		
    	MozgRoute::Get('http')->Redirect(array('module' => 'admin', 'ctrl' => $this->GetSelfName(), 'out_media', 'ext_id' => $extId, 'ext_mod' => $extMod));	    	
    }
    
    public function OutExtModInBlock()
    {
    	$content = $this->OutExtMod();
    	if (!$content) return '';
    	
        $t = $this->GetTemplator();
        $t->title = 'Элемент';
        $t->content = $content;
        return $t->render('_right_block_template.phtml');    	
    }
    
    private function OutExtMod()
    {
        $extId = $this->GetExtId();
        $extMod = $this->GetExtMod();     	
    	if (!$extId) return '';
        
  		$mod = MozgCfg::GetProp('global/media//extMods/' .$extMod);
		$extCtrl = $mod['ctrl'];
		
		$elelentBlock = '';
		if ($extCtrl && $this->_ctrls->$extCtrl && method_exists($this->_ctrls->$extCtrl, 'GetExtElementBlock'))
		{
			$elelentBlock = $this->_ctrls->$extCtrl->GetExtElementBlock($extId);
		}    	
    	
        $t = $this->GetTemplator();
        $t->ctrlName = $this->GetSelfName();
        $t->elementBlock = $elelentBlock;
        $t->extId = $extId;
        $t->extMod = $extMod;        
        $t->thumbFolder = $this->GetPhotoThumbFolder('mediathumbnail');
        $t->files = $this->GetMediaMapper()->GetExtMedia($extId, $extMod);
        return $t->render('media/_out_element_block.phtml');   
    }
    
    public function media_blockScAction($baseUrlParams, $params)
    {
        $t = $this->GetTemplator();
        $t->title = 'Медиа файлы';
        $t->content = $this->OutMediaBlock($baseUrlParams, $params);
        return $t->render('_left_block_template.phtml');
    }

    public function OutMediaBlock($baseUrlParams, $params = array())
    {
        $t = $this->GetTemplator();
        $t->baseUrlParams = $baseUrlParams;
        $t->filter = $this->OutFilter('media_block', $baseUrlParams);
        $t->filesBlock = $this->OutMediaFilesBlock($baseUrlParams, $params);
        return $t->render('media/_media_block.phtml');    		
    }         
    
    private function OutFilter($prefixName = 'media', $baseUrlParams = false)
    {
    	if (!$baseUrlParams)
    	{
    		$baseUrlParams = array(
    								'module' => 'admin',
    								'ctrl' => $this->GetSelfName(),
    								'action' => 'out_media',
    		);
    	}
    	
        $t = $this->GetTemplator();
        $t->baseUrlParams = $baseUrlParams;
        $t->filter = $this->GetFilter($prefixName);
        return $t->render('media/_filter_form.phtml');     	
    }

    public function OutFilterInBlock()
    {
        $t = $this->GetTemplator();
        $t->title = 'Фильтр';
        $t->content = $this->OutFilter();
        return $t->render('_left_block_template.phtml');
    }    
    
    protected function LoadTemplate ($content, $width = false)
    {
        $t = $this->GetTemplator();
        $t->title = $this->actionTitle;
        $t->content = $content;
        $t->width = $width;        
        $this->Response()->SetBody($t->render('_main_block_template.phtml'));
    }     
}